package com.noble.meether.ui.reg.nav;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.third.ThirdConstant;
import com.noble.library.utils.NoDoubleClickListener;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.chat.ui.imui.util.IMChatUtil;
import com.noble.meether.logic.third.auth.OauthBack;

/**
 * 注册登陆页面
 */
public class NavUserAct extends BaseAppCompatActivity {
    private OauthBack oauthBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isCanBack(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p1_nav_act);
        initView();
        initCallback();
    }

    private void initCallback() {
        if (oauthBack == null) {
            oauthBack = new OauthBack(this);
            ModuleMgr.getAuthMgr().setCallback(oauthBack);
        }
    }

    private void initView() {
        IMChatUtil.getInstance().clearMsgDBList();  // 清除信箱记录
        findViewById(R.id.login_qq).setOnClickListener(clickListener);
        findViewById(R.id.login_wx).setOnClickListener(clickListener);
        findViewById(R.id.login_phone).setOnClickListener(clickListener);
        findViewById(R.id.reg_protocol).setOnClickListener(clickListener);
    }

    private NoDoubleClickListener clickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            initCallback();
            switch (v.getId()) {
                case R.id.login_phone:
                    MeetUIShow.showRegMobileDlg(NavUserAct.this);
                    break;

                case R.id.login_wx:
                    LoadingDialog.show(NavUserAct.this, "请稍候");
                    ModuleMgr.getAuthMgr().onLogin(ThirdConstant.WECHAT);
                    break;
                case R.id.login_qq:
                    LoadingDialog.show(NavUserAct.this, "请稍候");
                    ModuleMgr.getAuthMgr().onLogin(ThirdConstant.QQ);
                    break;
                case R.id.reg_protocol:
                    UIShow.showRegisterAgree(NavUserAct.this);
                    break;
            }
        }
    };

    @Override
    public void onBackPressed() {
        ModuleMgr.getLoginMgr().clearCookie();
        super.onBackPressed();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (ThirdConstant.mPlatform == ThirdConstant.WECHAT)
            LoadingDialog.closeLoadingDialog();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ModuleMgr.getAuthMgr().release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // QQ登录回调
        ModuleMgr.getAuthMgr().onActivityResult(requestCode, resultCode, data);
    }
}
